package modes

import (
	"fmt"
)

/*题型别名*/
type STypeRe struct {
	Id    int64  `json:"id" xorm:"id"`         //id
	SType int64  `json:"s_type" xorm:"s_type"` //通用类型id
	Name  string `json:"name" xorm:"name"`     //类型别名名称
}

func (this *STypeRe) TableName() string {
	return "s_type_re"
}

func (this *STypeRe) delete(where string) (int64, error) {
	return Db(0).Where(where).Delete(this)
}
func (this *STypeRe) Del() (int64, error) {
	where := fmt.Sprintf("id =%d", this.Id)
	return this.delete(where)
}
func (this *STypeRe) Count(where string) (int64, error) {
	var val Record
	return Db(0).Where(where).Count(&val)
}
func (this *STypeRe) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *STypeRe) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *STypeRe) where(fage, count, page int, where, field string) ([]STypeRe, error) {
	list := make([]STypeRe, 0)
	var err error
	if field == "" {
		field = "id"
	}
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

func (this *STypeRe) GetList(tidList []int64) ([]int64, error) {
	list := make([]int64, 0)
	err := Db(0).Table("s_type_re").Find(&list)
	return list, err
}
func (this *STypeRe) GetAllList() ([]STypeRe, error) {
	list := make([]STypeRe, 0)
	err := Db(0).Find(&list)
	return list, err
}

type STTypeRe struct {
	SType       int    `json:"s_type" xorm:"s_type2"`                //题库基础类型
	STypeName   string `json:"s_type_name" xorm:"s_type_name"`       //题库基础名称
	STypeRe     int    `json:"s_type_re" xorm:"s_type_re2"`          //题库别名ID
	STypeReName string `json:"s_type_re_name" xorm:"s_type_re_name"` //题库别名名称
	Type        int    `json:"type" xorm:"type"`                     //题型类型
}

func (this *STypeRe) GetSTypeList() ([]STTypeRe, error) {
	list := make([]STTypeRe, 0)
	err := Db(0).Table("s_type_re").Select("s_type_re.id  s_type_re2,s_type_re.`name` s_type_re_name,s_type.id s_type2,s_type.`name` s_type_name,s_type.type").
		Join("LEFT", "s_type", "s_type_re.s_type=s_type.id").Find(&list)
	return list, err
}
